package com.groupseven.backend.configuration.shiro;

import com.groupseven.backend.domain.ResultType;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class NoPermissionException {
    // 没有权限时抛出的异常
    @ResponseBody
    @ExceptionHandler(UnauthorizedException.class)
    public ResultType handleShiroException(HttpServletResponse resp) throws IOException {
        return new ResultType(0,"无权限！",601);
    }

    // 权限校验失败时抛出的异常
    @ResponseBody
    @ExceptionHandler(AuthorizationException.class)
    public ResultType AuthorizationException(HttpServletResponse resp) throws IOException {
        return new ResultType(0,"权限校验失败，请重新登录！",602);
    }

    @ResponseBody
    @ExceptionHandler(IncorrectCredentialsException.class)
    public ResultType IncorrectCredentialsException(HttpServletResponse resp) throws IOException {
        return new ResultType(0,"密码错误！",603);
    }

    @ResponseBody
    @ExceptionHandler(UnknownAccountException.class)
    public ResultType UnknownAccountException(HttpServletResponse resp) throws IOException {
        return new ResultType(0,"账号不存在！",604);
    }
    @ResponseBody
    @ExceptionHandler(ExpiredCredentialsException.class)
    public ResultType ExpiredCredentialsException(HttpServletResponse resp) throws IOException {
        return new ResultType(0,"token过期！",604);
    }

}
